<?php

/**
 * 数据操作类
 */
class Request
{
    //允许的请求方式
    private static $method_type = array('get', 'post', 'put', 'patch', 'delete');
    
    //连接信息
    private static $conn;

    //创建数据库的连接
    public static function CreateConn()
    {
        //连接信息初始化
        self::$conn = mysqli_connect("18codeteam.cn","root","ruanJian18","canting");
        //设置字符集utf8
        mysqli_query(self::$conn,"set Names utf8"); 
    }

    //根据请求方式调用不同函数
    public static function getRequest()
    {
        //获取转为小写的请求方式
        $method = strtolower($_SERVER['REQUEST_METHOD']);

        //如果请求方式在规定的范围里
        if (in_array($method, self::$method_type)) {
            $data_name = $method . 'Data';
            //调用请求方式对应的方法,（将$_REQUEST数组当做参数，$_REQUEST数组中保存着我们传过来的参数）
            return self::$data_name($_REQUEST);
        }
        //否则返回false
        return false;
    }

    //GET 获取数据，根据传过来的参数获取相应的数据
    private static function getData($request_data)
    {        
            //获取menus表中所有数据
        	if ($request_data['data'] == "allMenuData"){
        		$sql ="select * from Menus ";
            //获取所有分类数据
        	}else if($request_data['data'] == "fenleiData"){
        		$sql = "select * from fenlei";
            //获取所有食材数据
        	}else if($request_data['data'] == "shicaiData"){
                $sql = "select * from foods";
            //获取menus表中某道菜品的数据
            }else if(!empty($request_data['menu_id'])){
                $sql = "select * from Menus where menu_id=".$request_data['menu_id'];
            //获取mf表（中间表）里某道菜的所有食材名称，并将食材名称放入数组里
            }else if($request_data['data']=="getfood" && !empty($request_data['menu_name'])){
                $menu_name = $request_data['menu_name'];
                $sql = "select f_name from mf where m_name ='$menu_name'";
                $result = mysqli_query(self::$conn,$sql);
                $foodList = array();
                while ($re = mysqli_fetch_array($result)) {
                    $foodList[] = $re['f_name'];
                }
                return $foodList;
            //获取mf表中某道菜的所有食材数据
            }else{
        		$menu_name = $request_data['data'];
        		$sql = "select * from mf where m_name = '$menu_name'";
        	}
            
            $result = mysqli_query(self::$conn,$sql);
            return $result;
        
    }
    //添加菜品信息
    private static function postData($request_data)
    {
        
        $menu_name = $request_data['menu_name'];
        $menu_unit_price = $request_data['menu_unit_price'];
        $menu_weight = $request_data['menu_weight'];
        $menu_spicy = $request_data['menu_spicy'];
        $menu_special = $request_data['menu_special'];
        $menu_size = $request_data['menu_size'];
        $menu_cook_type = $request_data['menu_cook_type'];
        $menu_veg_meat = $request_data['menu_veg_meat'];
        $menu_cook_time = $request_data['menu_cook_time'];

        //将传递过来的菜品数据插入到menus表中
        $sql_insert_menus = "INSERT INTO menus (menu_name,menu_unit_price,menu_weight,menu_spicy,menu_special,menu_size,menu_cook_type,menu_veg_meat,menu_cook_time)VALUES('$menu_name',$menu_unit_price,$menu_weight,$menu_spicy,'$menu_special','$menu_size','$menu_cook_type','$menu_veg_meat',$menu_cook_time)";
        mysqli_query(self::$conn,$sql_insert_menus);

        //将传递过来的食材数据插入到mf表中
        $food_array = explode(',', $request_data['menu_food_materia']);
        foreach ($food_array as $food) {
            $sql_insert_foods = "INSERT INTO mf(m_name,f_name)VALUES('$menu_name','$food')";
            mysqli_query(self::$conn,$sql_insert_foods);
        }
        
        //返回添加后的menus表中所有数据
        $sql_query = "select * from menus";
        $result = mysqli_query(self::$conn,$sql_query);
        return $result;
    }

    //菜品数据更新
    public static function putData($request_data)
    {
        $menu_name = $request_data['menu_name'];
        $menu_unit_price = $request_data['menu_unit_price'];
        $menu_weight = $request_data['menu_weight'];
        $menu_spicy = $request_data['menu_spicy'];
        $menu_special = $request_data['menu_special'];
        $menu_size = $request_data['menu_size'];
        $menu_cook_type = $request_data['menu_cook_type'];
        $menu_veg_meat = $request_data['menu_veg_meat'];
        $menu_cook_time = $request_data['menu_cook_time'];
        $menu_id = $request_data['menu_id'];

        //更新menus表中菜品数据
        $sql_update_menus = "UPDATE menus set menu_name = '$menu_name',menu_unit_price='$menu_unit_price',menu_weight='$menu_weight',menu_spicy='$menu_spicy',menu_special='$menu_special',menu_size='$menu_size',menu_cook_type='$menu_cook_type' where menu_id=".$menu_id;
        mysqli_query(self::$conn,$sql_update_menus);

        //更新mf表中食材数据，先将所有原先食材删除
        $sql_delete_menufood = "delete from mf where m_name = '".$request_data['menu_name']."'";
        mysqli_query(self::$conn,$sql_delete_menufood);
        //再讲传递过来的食材数据插入到mf表
        $food_array = explode(',', $request_data['menu_food_materia']);
        foreach ($food_array as $food) {
            $sql_insert_foods = "INSERT INTO mf(m_name,f_name)VALUES('$menu_name','$food')";
            mysqli_query(self::$conn,$sql_insert_foods);
        }
        

        //返回更新后的menus表中所有数据
        $sql_query = "select * from menus";
        return mysqli_query(self::$conn,$sql_query);

    }

    //删除菜品
    public static function deleteData($request_data)
    {
        //根据传递过来的数据进行删除
        $sql_delete_menus = "delete from menus where menu_id = ".$request_data['menu_id'];
        mysqli_query(self::$conn,$sql_delete_menus);
        //再讲mf表中这道菜的所有食材数据删除
        $sql_delete_menufood = "delete from mf where m_name = '".$request_data['menu_name']."'";
        mysqli_query(self::$conn,$sql_delete_menufood);

        //返回删除后的menus表中所有数据
        $sql_query = "select * from menus";
        return mysqli_query(self::$conn,$sql_query);
    }

    
}











